
"""
__version__ = "$Revision: 1.3 $"
__date__ = "$Date: 2004/08/12 19:14:23 $"
"""
from traceback import print_exc 
import pickle 

from PythonCard import model, dialog
from Brain.knowledge_database.match import MatchConfiguration
from Brain.planer.main import Planer 
from Brain.plan import Plan

class GameConfigurationDialog(model.CustomDialog):
	def __init__(self, parent, configuration):
		model.CustomDialog.__init__(self, parent)
		if configuration:
			self._configuration = configuration.copy()
		else:
			self._configuration = MatchConfiguration()
		self.refresh_all()
	
	def refresh_all(self):
		self.refresh_list()
		self.components.color_choice.stringSelection = unicode(self._configuration.our_color)
		self.components.start_x.text = unicode(self._configuration.start_x)
		self.components.start_y.text = unicode(self._configuration.start_y)
		self.components.start_angle.text = unicode(self._configuration.start_angle)
	
	def refresh_list(self):
		## populate list widget ..
		self.components.plan_collection.clear()
		for plan in self._configuration.plans:
			description = str(plan)
			self.components.plan_collection.append( description )
	
	def process(self):
		""" return configuration from user input
			on save btn click
		"""
		self._configuration.our_color = str( self.components.color_choice.stringSelection )
		self._configuration.start_x = str( self.components.start_x.text )
		self._configuration.start_y = str( self.components.start_y.text )
		self._configuration.start_angle = str( self.components.start_angle.text )
		return self._configuration.copy()
	
	def on_new_plan_mouseClick(self, event):
		""" add new plan """
		new = Plan()
		planer = Planer( new )
		try:
			planer.run()
		except:
			print_exc()
			
		self._configuration.plans.append( new )
		self.refresh_list()

	def on_edit_plan_mouseClick(self, event):
		""" edit plan """
		for index in self.components.plan_collection.GetSelections():
			if 0 <= index < len(self._configuration.plans):
				plan = self._configuration.plans[index]
				planer = Planer( plan )
				try:
					planer.run()
				except:
					print_exc()
		self.refresh_list()
	
	def on_add_plan_mouseClick(self, event):
		""" add plan from file """
		try:
			result = dialog.openFileDialog(wildcard='*.pln;*.plan')
			if result.accepted:
				filepath = result.paths[0]
				loaded_plan = pickle.load(open(filepath, 'rb'))
				self._configuration.plans.append( loaded_plan )
				self.refresh_list()
		except:
			print ' ERROR loading failed !'

	def on_remove_plan_mouseClick(self, event):
		""" remove selected plans """
		for index in self.components.plan_collection.GetSelections():
			if 0 <= index < len(self._configuration.plans):
				self._configuration.plans.pop(index)
			self.refresh_list()
			return 

	def on_load_mouseClick(self, event):
		try:
			result = dialog.openFileDialog(wildcard='*.match')
			if result.accepted:
				filepath = result.paths[0]
				loaded_configuration = pickle.load(open(filepath, 'rb'))
				print loaded_configuration
				self._configuration = loaded_configuration
				self.refresh_all()
		except:
			print_exc()
			print ' ERROR loading failed !'


def execute_gameconfiguration_dialog(object_to_edit):
	dlg = GameConfigurationDialog(None, object_to_edit)
	result = dlg.showModal()
	if result.accepted:
		result.edited = dlg.process()
		print 'accepted ', result.edited
	else : 
		result.edited = None
	dlg.destroy()
	return result


if __name__ == '__main__':
	class TesT(model.Background):
		pass
	
	app = model.Application(TesT, r'minimal_app.rsrc.py')
	ret = execute_gameconfiguration_dialog(None)
	print ret.edited






